package com.unei.mq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by sunlei on 2015/8/11.
 */
public class TopicConsumer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory;
        Connection connection=null;
        Session session=null;
        Destination destination;
        MessageConsumer consumer;
        connectionFactory=new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");

        try{
            connection=connectionFactory.createConnection();
            connection.start();
            session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
            destination=session.createTopic("FirstTopic");
            consumer=session.createConsumer(destination);
            while(true){
                TextMessage message=(TextMessage)consumer.receive();
                if(null!=message){
                    System.out.println("receive message:"+message.getText());
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(connection!=null)
                connection.close();
        }
    }
}
